/*
 * Copyright (c) [2019] [lookang]
 *    [Software Name] is licensed under the Mulan PSL v1.
 *    You can use this software according to the terms and conditions of the Mulan PSL v1.
 *    You may obtain a copy of Mulan PSL v1 at:
 *       http://license.coscl.org.cn/MulanPSL
 *    THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 *    IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 *    PURPOSE.
 *    See the Mulan PSL v1 for more details.
 */

package com.lookang.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.base.Strings;
import com.lookang.demo.entity.User;
import com.lookang.demo.service.UserService;
import com.lookang.demo.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lookang
 * @since 2019-11-22
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/registerPage", method = RequestMethod.GET)
    public String registerPage() {
        return "register";
    }

    @ResponseBody
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public ResultVO register(@RequestBody User user) {
        String userName = user.getUserName();
        if (Strings.isNullOrEmpty(userName)) {
            return ResultVO.failed("用户名不能为空");
            //123
        }
        int count = userService.count(new QueryWrapper<User>().lambda().eq(User::getUserName, userName));
        if (count>0){
            return ResultVO.failed("用户名已存在");
        }
        userService.save(user);
        return ResultVO.success();
    }


}
